package com.good.jzhouse.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: xiaolei
 * @Date: 2021/3/13 10:24
 */
@Configuration
public class shiroConfig{
    @Bean("shiroFilterFactoryBean")     //创建请求管理器
    public ShiroFilterFactoryBean getShiroFactoryBean(@Qualifier("DefaultWebSecurityManager")DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(manager);
        //配置首    先资源
        Map<String,String> map = new LinkedHashMap<>();
        map.put("/css/**", "anon");//css
        map.put("/images/**", "anon");//images
        map.put("/js/**", "anon");//js
        map.put("/layui/**", "anon");// layui组件
        map.put("/publichtml/**", "anon");// 公共页面

        //controller
        map.put("/","anon"); //登录页面
        map.put("/public/**","anon"); //登录页面
        map.put("/jump/**","anon");   //首页
        map.put("/selhouse/**","anon");   //房屋查询
        map.put("/proinfo/**","anon");   //房屋查询
        map.put("/properties/**","anon");   //首页房屋查询
        map.put("/Adviser/**","anon");
        map.put("/comm/**","anon");
        map.put("/dataimg/**","anon");
        map.put("/Market/**","anon");
        map.put("/Presell/**","anon");
        map.put("/recharge/**","anon");
        map.put("/sellhouse/**","anon");
        map.put("/xinxi/**","anon");
        //usersController里面的权限
        map.put("/backer/login","anon");//登录
        map.put("/backer/checkPhoneNumber","anon");//判断手机号是否存在
        map.put("/backer/register","anon");//提交注册
        map.put("/backer/logout", "anon");//退出
        map.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setLoginUrl("/public/login");
        return  shiroFilterFactoryBean;
    }

    @Bean("RayRealm")
    public RayRealm getRayRealm(){
        System.out.println("getRayRealm..............");
        RayRealm rayRealm = new RayRealm();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashIterations(1024);
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        rayRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return rayRealm;
    }

    @Bean("DefaultWebSecurityManager")      //创建web安全管理器
    public DefaultWebSecurityManager getWebSecurityManager(@Qualifier("RayRealm") Realm realm){
        System.out.println("getWebSecurityManager......................");
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        return  defaultWebSecurityManager;
    }


}
